import cv2
import numpy as np

# 读取3D图像
img = cv2.imread('./images/Barbara.tif', cv2.IMREAD_ANYDEPTH)

# 设定阈值
threshold = 50

# 进行阈值分割
ret, thresh = cv2.threshold(img, threshold, 255, cv2.THRESH_BINARY)

img_display = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
thresh_display = cv2.cvtColor(thresh, cv2.COLOR_GRAY2BGR)

# 获取图像尺寸
h, w = img_display.shape[:2]

# 创建水平拼接的图像
combined = np.zeros((h, w*2, 3), dtype=np.uint8)
combined[:, :w] = img_display
combined[:, w:] = thresh_display

# 添加标题
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(combined, 'Original 3D Image', (10, 30), font, 1, (255, 255, 255), 2)
cv2.putText(combined, 'Thresholded Image', (w+10, 30), font, 1, (0, 0, 0), 2)

# 显示拼接后的图像
cv2.imshow('3D Image Segmentation Results', combined)
cv2.waitKey(0)
cv2.destroyAllWindows()